using System;
using System.IO;
using System.Windows;
using System.Diagnostics;
using WikiApp.Pages;

namespace WikiApp
{
    public partial class App : Application
    {
        public App()
        {
            Console.WriteLine("App constructor called");
            Debug.WriteLine("App constructor called");
            
            // 添加未处理异常处理器
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            // 确保输出被立即显示
            Debug.AutoFlush = true;
            Console.WriteLine("Debug.AutoFlush set to true");
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                Console.WriteLine("应用程序启动...");
                Debug.WriteLine("应用程序启动...");

                // 检查当前目录
                var currentDir = Directory.GetCurrentDirectory();
                Console.WriteLine($"当前目录: {currentDir}");
                Debug.WriteLine($"当前目录: {currentDir}");

                // 检查配置文件
                var configPath = Path.Combine(currentDir, "Config", "pages.json");
                Console.WriteLine($"配置文件路径: {configPath}");
                Debug.WriteLine($"配置文件路径: {configPath}");

                if (!Directory.Exists(Path.Combine(currentDir, "Config")))
                {
                    throw new DirectoryNotFoundException($"Config 目录不存在: {Path.Combine(currentDir, "Config")}");
                }

                if (!File.Exists(configPath))
                {
                    throw new FileNotFoundException($"配置文件不存在: {configPath}");
                }

                base.OnStartup(e);

                Console.WriteLine("初始化 CategoryPageFactory...");
                Debug.WriteLine("初始化 CategoryPageFactory...");
                CategoryPageFactory.Initialize();

                Console.WriteLine("创建主窗口...");
                Debug.WriteLine("创建主窗口...");
                var window = new MainWindow();
                MainWindow = window;  // 设置主窗口

                Console.WriteLine("显示主窗口...");
                Debug.WriteLine("显示主窗口...");
                window.Show();

                Console.WriteLine("应用程序启动完成");
                Debug.WriteLine("应用程序启动完成");
            }
            catch (Exception ex)
            {
                var error = $"启动时发生错误：\n{ex.Message}\n\n{ex.StackTrace}";
                Console.WriteLine(error);
                Debug.WriteLine(error);
                MessageBox.Show(error, "严重错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Shutdown(-1);
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            var error = $"UI线程未处理异常：\n{e.Exception.Message}\n\n{e.Exception.StackTrace}";
            Console.WriteLine(error);
            Debug.WriteLine(error);
            MessageBox.Show(error, "未处理的UI异常", MessageBoxButton.OK, MessageBoxImage.Error);
            e.Handled = true;
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var error = $"应用程序未处理异常：\n{(e.ExceptionObject as Exception)?.Message}\n\n{(e.ExceptionObject as Exception)?.StackTrace}";
            Console.WriteLine(error);
            Debug.WriteLine(error);
            MessageBox.Show(error, "未处理的异常", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
} 